<?
/**
*  Pager Link Bar 
*  Author : Jammy 
*  Date   : 2008-09-09
*/


class PageLinks 
{

   var $recordCount  = 0;       // 记录总数
   var $pageSize     = 0;       // 每页显示记录数
   var $linkStyle    = '';      // 链接文字风格样式

   function PageLinks($recordCount, $pageSize, $linkStyle='')  {
	  $this->recordCount  = $recordCount;
	  $this->pageSize     = $pageSize;
	  $this->linkStyle    = $linkStyle;
   }

   function setRecordCount($v) {
	  $this->recordCount = $v;
   }

   function setPageSize($v) {
	  $this->pageSize = $v;
   }

   function show($returnHtml=false) {
      $pageUrl = $_SERVER["PHP_SELF"];
	  $currentPage = 1;
	  if($_GET["page"]) {
		$currentPage = intval($_GET["page"]);
	  }
	  $httpParams = $this->getHttpParams();
	  $html = "";

	  $pageCount = ceil($this->recordCount / $this->pageSize);
	  if ( $currentPage > $pageCount ) {
	     $currentPage = $pageCount; 
	  } 
	  
	  $pageLinkPerGroup  =  10;   // 每组显示分页链接数目
	  $groupCount =  ceil($pageCount / $pageLinkPerGroup);          // 分组总数
	  $currentGroup =  intval(($currentPage-1) / $pageLinkPerGroup);// 当前页分组号
	  $groupStartPageNo  = $pageLinkPerGroup * $currentGroup + 1;   // 当前分组起初页号
	 

	  $html .= sprintf("Total <B>%s</B> pages　", $pageCount);

	  if( $currentGroup > 0 ) {
			$html .= sprintf("<a href='%s?page=%s&%s' %s title='prev %s pages'><font style='font-family:webdings; text-decoration:none'>7</font></a>&nbsp;", $pageUrl, $groupStartPageNo - $pageLinkPerGroup , 
			$httpParams, $this->linkStyle, $pageLinkPerGroup);
	  }

	  $i = $groupStartPageNo;
	  $j = $i + $pageLinkPerGroup;
	  for ( ; ($i<$j) && ($i<=$pageCount); $i++) {
			if ($currentPage == $i) {
			  $html .= sprintf("<font color=red><b>%s</b></font>&nbsp;", $i);
			} else {
			  $html .= sprintf("<a href='%s?page=%s&%s' %s>%s</a>&nbsp;", $pageUrl, $i, $httpParams, 
				  $this->linkStyle, $i);
			}
	  }

	  if( $currentGroup < $groupCount-1 ) {
			$html .= sprintf("<a href='%s?page=%s&%s' %s  title='next %s pages'><font style='font-family:webdings; text-decoration:none'>8</font></a>&nbsp;", $pageUrl, $groupStartPageNo + $pageLinkPerGroup , 
			$httpParams, $this->linkStyle, $pageLinkPerGroup);
	  }
	 
	  $html .= $this->getJumpBoxHtml($pageCount);

	  if( $returnHtml ) {
		   return $html;
	  } else {
		   echo $html;
	  }

   }


   function getHttpParams() {
	   $params = array();  
	   if( !empty($_POST) ) {
		  foreach( $_POST as $k => $v) {
			if( ($k!="page") && ($v!="") ) {
			   $params[] = "$k=$v";
			}
		  }

	   } elseif( !empty($_GET) ) {
		  foreach( $_GET as $k => $v) {
			if( ($k!="page") && ($v!="") ) {
			   $params[] = "$k=$v";
			}
		  }
       }
	   return join("&", $params);
   }


   function getJumpBoxHtml($pageCount) {
	     $randId = "jump_page_num_".rand(1, 1000);
		 $html = "&nbsp;To:<input type='text' size=1 name='{$randId}' style='height:10px;width:16px; border:black 1px solid; background:#f4f4f4' onkeydown='go_to_page(this.value)'><script>function go_to_page(v){var pageNo=v.replace(/[\s　\D]+/g, ''); if(event.keyCode==13 && pageNo!='' && parseInt(pageNo)<={$pageCount}){ var url = window.location.toString(); if(url.indexOf('page=') > 0) {url = url.replace(/\page=[\d]+/g, 'page='+pageNo)} else if(url.indexOf('?') > 0) {url += '&page='+pageNo} else {url += '?page='+pageNo}; window.location = url }}</script>";  
		 return $html;
   }

} 

/*
// Sample Code

$pager = new Pager(200, 10);
$pager->show();
$pager->show();
*/
?>